package com.linrung.gdsfrz;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.linrung.core.cache.ICacheClient;
import com.linrung.core.utils.AjaxUtils;
import com.linrung.core.utils.LoggerUtils;

@Controller
public class OrgSyncTaskAction {

	private LoggerUtils logger = LoggerUtils.getLogger(OrgSyncTaskAction.class);
	
	@Autowired
	private ICacheClient iCacheClient;
	
	@Autowired
	private RedisTemplate<String, Object> redisTemplate;
	
	/** 缓存key：待同步的机构id，会同步本身和递归所有子级的机构 **/
	public static final String CACHE_KEY_TODO_ID_LIST = "task:gdex:unit:todoid";
	
	/** 缓存key：待同步的机构id，只同步本身信息 **/
	public static final String CACHE_KEY_TODO_ID_SIGN_LIST = "task:gdex:unit:todoid:sign";
	
	/** 缓存key：待同步的机构id，标记同步整个机构下的用户 **/
	public static final String CACHE_KEY_UPDATE_USER_UNIT_ID_LIST = "task:gdex:user:todo:unitid";
	
	/**
	 * 发送同步信息的任务，由定时程序去执行
	 */
	@RequestMapping(value="/org/sendSyncTask")
	@ResponseBody
	public void sendSyncTask(Integer type, String unitId,
			HttpServletRequest request, HttpServletResponse response){
		if(null!=type && StringUtils.isNotBlank(unitId)){
			switch (type) {
				case 1:
					redisTemplate.opsForList().rightPush(CACHE_KEY_TODO_ID_SIGN_LIST, unitId);
					break;
				case 2:
					redisTemplate.opsForList().rightPush(CACHE_KEY_TODO_ID_LIST, unitId);
					break;
				case 3:
					redisTemplate.opsForList().rightPush(CACHE_KEY_UPDATE_USER_UNIT_ID_LIST, unitId);
					break;
				default:
					break;
			}
			AjaxUtils.writeResponseHtmlInfo(true, null, null, null, response);
		}else{
			AjaxUtils.writeResponseHtmlInfo(false, "关键参数为空", null, null, response);
		}
	}
}
